//
//  SQAddReleaseViewController.m
//  energy
//
//  Created by 苏青 on 2021/9/29.
//

#import "SQAddReleaseViewController.h"
/**应急预案名称*/
#import "SQAccidentTypesViewController.h"

#import "SQAccidentTypesModel.h"
#import "SQAddReleaseModel.h"

@interface SQAddReleaseViewController ()
@property ( strong ,nonatomic) NSArray *adjustmentArray;
@property ( strong ,nonatomic) NSString *er_grade_id;
/**应急预案*/
@property (weak, nonatomic) IBOutlet UILabel *lab_plan;
/**应急响应级别*/
@property (weak, nonatomic) IBOutlet UITextField *tf_level;

/**应急预案id*/
@property ( strong ,nonatomic) NSArray *accidentTypesArray;
@property (weak, nonatomic) IBOutlet UIButton *btn_type;

/**,应急预案id集合*/
@property ( copy ,nonatomic) NSString *er_plan_id;
@property ( copy ,nonatomic) NSString *er_plan_text;
/**应急预案负责人id集合*/
@property ( copy ,nonatomic) NSString *er_responsible_id;
@property ( copy ,nonatomic) NSString *er_responsible_text;

@end

@implementation SQAddReleaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发布应急响应";
    self.adjustmentArray = @[@"I级",@"II级",@"III级",@"IV级"];
    self.er_grade_id = @"0";
    self.er_plan_id = @"0";
    self.er_plan_text = @"";
    self.er_responsible_id = @"0";
    self.er_responsible_text = @"";
}
#pragma mark -- 紧急预案
- (IBAction)clickPlan:(UIButton *)sender {
    [self.view endEditing:YES];
    SQAccidentTypesViewController * accidentTypesVC = SQAccidentTypesViewController.new;
    
    if (self.accidentTypesArray.count != 0) {
        accidentTypesVC.accidentTypeIDs = self.accidentTypesArray;
        SQ_LOG(@"dddddddcc    %@",self.accidentTypesArray);
    }
    accidentTypesVC.accidentTypesBack = ^(id  _Nonnull str, id  _Nonnull josn, id  _Nonnull accidentTypesModel) {
        NSMutableArray * arrayMName = NSMutableArray.new;
        NSMutableArray * arrayMID = NSMutableArray.new;

        for (SQAccidentTypesModelData * dataModel in accidentTypesModel) {
            SQ_LOG(@"%@",dataModel);
            for (SQAccidentTypesModelPerson * personModel in dataModel.person) {
                [arrayMID addObject:personModel.epp_ep_person_id];
                [arrayMName addObject:personModel.epp_ep_person_text];
            }
        }
        self.er_responsible_id = [arrayMID componentsJoinedByString:@","];
    //
        self.er_responsible_text = [arrayMName componentsJoinedByString:@","];
        self.lab_plan.text =  str;
        self.accidentTypesArray = accidentTypesModel;
        self.er_plan_id = josn;
        self.er_plan_text = str;
        [self.btn_type setTitle:@"" forState:UIControlStateNormal];
    };
    [self pushVC:accidentTypesVC];
}
#pragma mark -- 应急响应级别
- (IBAction)clickLevel:(UIButton *)sender {
  
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"应急响应等级"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i < self.adjustmentArray.count; i ++) {
      
        UIAlertAction* adjustmentAction = [UIAlertAction actionWithTitle:self.adjustmentArray[i] style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                
            if ([self.adjustmentArray[i] isEqualToString:@"I级"]) {
                self.er_grade_id = @"1";
            }else if ([self.adjustmentArray[i] isEqualToString:@"II级"]){
                self.er_grade_id = @"2";
            }else if ([self.adjustmentArray[i] isEqualToString:@"III级"]){
                self.er_grade_id = @"3";
            }else if ([self.adjustmentArray[i] isEqualToString:@"IV级"]){
                self.er_grade_id = @"4";
            }
            self.tf_level.text = self.adjustmentArray[i];

          
        }];
        [alert addAction:adjustmentAction];
    }
    
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
        NSLog(@"action = %@", action);
    }];
    [alert addAction:cancelAction];
//
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark -- 发布应急响应
- (IBAction)clickAddRelease:(UIButton *)sender {
    if ([self verifications]) {
//        /**增加或修改应急响应。传参：主键(er_id)-传0则新增、传id则修改,应急响应等级id(er_grade_id),应急响应等级中文(er_grade_text),应急预案id集合(er_plan_id),应急预案中文集合(er_plan_text),应急预案负责人id集合(er_responsible_id),应急预案负责人中文集合(er_responsible_text),状态(er_status),发布人(er_publish_person),发布人组织架构(er_publish_organize),企业id(er_enterprise)*/
        [SQAddReleaseModel getDataWithParams:@{@"er_id":@"0",@"er_grade_id":self.er_grade_id,@"er_grade_text":self.tf_level.text,@"er_plan_id":self.er_plan_id,@"er_plan_text":self.er_plan_text,@"er_responsible_id":self.er_responsible_id,@"er_responsible_text":self.er_responsible_text,@"er_status":@"0",@"er_publish_person":MName,@"er_publish_organize":[self join:@[[self isEmptyNull:MOneText],[self isEmptyNull:MTwoText],[self isEmptyNull:MThreeText],[self isEmptyNull:MFourText]]],@"er_enterprise":MEnterpriseId,@"er_alarm_person":self.apr_alarm_person} WithCallBack:^(SQAddReleaseModel * data) {
            
            [CCToast showMessage:@"应急响应发布成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"addRelease" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
   
    
}

#pragma mark ---- 验证
- (BOOL)verifications{
    BOOL ret = NO;
    if([self.lab_plan.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择应急预案"];
        return NO;
    }

    else if([self.tf_level.text isEqualToString:@""]){
          
        [CCToast showMessage:@"请选择应急响应等级"];
           return NO;
    }else{
        ret = YES;
    }
    return YES;
}


- (NSString *)join:(NSArray<NSString *> *)strs{
    NSString *str = @"";
    for (NSString *key in strs) {
        if (key.length > 0) {
            str = [str stringByAppendingFormat:@"%@-",key];
        }
    }
    if (!str.length)return @"";
    return [str substringToIndex:str.length - 1];
}

- (NSString *)isEmptyNull:(NSString *)string{
    if (string == nil || string == NULL) {
        string = @"";
    }
    return string;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
